package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.api.entity.ControllerBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;

import java.util.List;
import java.util.Map;

/**
    主机item样式
 */
public final class SmartControllerCategoryAdapter extends AppAdapter<Map<String, SparseArray<List<ControllerBean>>>> {

    public SmartControllerCategoryAdapter(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView tvCategory;
        private final RecyclerView normalRecyclerView;
        private final View stopControllerLayout;
        private final RecyclerView stopRecyclerView;
        private final View btnSwitchStopController;

        private ViewHolder() {
            super(R.layout.item_controller_sort);
            tvCategory = findViewById(R.id.tv_category);
            tvCategory.setSelected(true);
            normalRecyclerView = findViewById(R.id.normal_recycler_view);
            stopControllerLayout = findViewById(R.id.stop_controller_layout);
            btnSwitchStopController = findViewById(R.id.btn_switch_stop_controller);
            stopRecyclerView = findViewById(R.id.stop_recycler_view);
            btnSwitchStopController.setTag(stopRecyclerView);
            btnSwitchStopController.setOnClickListener(mOnClickListener);
            tvCategory.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    v.setSelected(!v.isSelected());
                    if(v.isSelected()){
                        normalRecyclerView.setVisibility(View.VISIBLE);
                        stopControllerLayout.setVisibility(View.VISIBLE);
                    }else{
                        normalRecyclerView.setVisibility(View.GONE);
                        stopControllerLayout.setVisibility(View.GONE);
                    }
                }
            });
        }

        @Override
        public void onBindView(int position) {
            Map<String, SparseArray<List<ControllerBean>>> item = getItem(position);
            String type = null;
            SparseArray<List<ControllerBean>> sparseArray = null;
            for(String key : item.keySet()){
                type = key;
                sparseArray = item.get(key);
            }
            List<ControllerBean> normal = null;
            List<ControllerBean> stop = null;
            if(sparseArray != null){
                normal = sparseArray.get(0);
                stop = sparseArray.get(1);
            }
            int num = normal.size() + stop.size();
            int onlineNum = 0;
            for(ControllerBean bean : normal){
                if(bean.getStateType() == 1){
                    onlineNum ++;
                }
            }
            tvCategory.setText(type + "(" + onlineNum +"/"+num +")");
            SmartControllerSubAdapter smartControllerSubAdapter = (SmartControllerSubAdapter) normalRecyclerView.getAdapter();
            if(smartControllerSubAdapter == null){
                smartControllerSubAdapter = new SmartControllerSubAdapter(getContext());
                normalRecyclerView.setAdapter(smartControllerSubAdapter);

                if (type.equals(getString(R.string.area_gateway))) {//区域主机 x个从机
                    GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 1);
                    gridLayoutManager.setOrientation(RecyclerView.VERTICAL);
                    normalRecyclerView.setLayoutManager(gridLayoutManager);
                }else {
                    GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);
                    gridLayoutManager.setOrientation(RecyclerView.VERTICAL);
                    normalRecyclerView.setLayoutManager(gridLayoutManager);
                }


            }
            smartControllerSubAdapter.setData(normal);
            if(stop == null || stop.size() == 0){
                stopControllerLayout.setVisibility(View.GONE);
            }else{
                stopControllerLayout.setVisibility(View.VISIBLE);
                SmartControllerSubAdapter stopSmartControllerSubAdapter = (SmartControllerSubAdapter) stopRecyclerView.getAdapter();
                if(stopSmartControllerSubAdapter == null){
                    stopSmartControllerSubAdapter = new SmartControllerSubAdapter(getContext());
                    stopRecyclerView.setAdapter(stopSmartControllerSubAdapter);

                    if (type.equals(getString(R.string.area_gateway))) {//区域主机 x个从机
                        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 1);
                        gridLayoutManager.setOrientation(RecyclerView.VERTICAL);
                        stopRecyclerView.setLayoutManager(gridLayoutManager);
                    }else {
                        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);
                        gridLayoutManager.setOrientation(RecyclerView.VERTICAL);
                        stopRecyclerView.setLayoutManager(gridLayoutManager);
                    }

                }
                stopSmartControllerSubAdapter.setData(stop);
            }
        }
    }

    @Override
    public long getItemId(int position) {
        return getData().get(position).hashCode();
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            v.setSelected(!v.isSelected());
            View stopRecyclerView = (View) v.getTag();
            if(v.isSelected()){
                stopRecyclerView.setVisibility(View.VISIBLE);
            }else{
                stopRecyclerView.setVisibility(View.GONE);
            }
        }
    };

}